﻿/*----------------------------------------------------------------
 * 版权所有 (c) 2025 HaiTangYunchi  保留所有权利
 * CLR版本：4.0.30319.42000
 * 公司名称：
 * 命名空间：SalaryCalculator.Repositories
 * 唯一标识：2acf33fb-88c3-47e3-a5ce-e0d5d8a3f657
 * 文件名：WorkRecordRepository
 * 
 * 创建者：海棠云螭
 * 电子邮箱：haitangyunchi@126.com
 * 创建时间：2025/5/10 20:19:22
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;

namespace SalaryCalculator
{
    /// <summary>
    /// 工作记录数据访问类
    /// </summary>
    public class WorkRecordRepository
    {
        /// <summary>
        /// 获取指定员工的未结算工作记录
        /// </summary>
        public List<WorkRecord> GetUnsettledRecords(int employeeId)
        {
            var records = new List<WorkRecord>();

            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                SELECT * FROM WorkRecords 
                WHERE EmployeeId = @EmployeeId AND IsSettled = 0 
                ORDER BY WorkDate, StartTime";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@EmployeeId", employeeId);

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            records.Add(new WorkRecord
                            {
                                Id = Convert.ToInt32(reader["Id"]),
                                EmployeeId = Convert.ToInt32(reader["EmployeeId"]),
                                WorkDate = DateTime.Parse(reader["WorkDate"].ToString()),
                                StartTime = TimeSpan.Parse(reader["StartTime"].ToString()),
                                EndTime = TimeSpan.Parse(reader["EndTime"].ToString()),
                                BreakMinutes = Convert.ToInt32(reader["BreakMinutes"]),
                                IsSettled = Convert.ToInt32(reader["IsSettled"]) == 1
                            });
                        }
                    }
                }
            }

            return records;
        }

        /// <summary>
        /// 添加工作记录
        /// </summary>
        public void AddWorkRecord(WorkRecord record)
        {
            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                INSERT INTO WorkRecords 
                (EmployeeId, WorkDate, StartTime, EndTime, BreakMinutes)
                VALUES (@EmployeeId, @WorkDate, @StartTime, @EndTime, @BreakMinutes)";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@EmployeeId", record.EmployeeId);
                    command.Parameters.AddWithValue("@WorkDate", record.WorkDate.ToString("yyyy-MM-dd"));
                    command.Parameters.AddWithValue("@StartTime", record.StartTime.ToString());
                    command.Parameters.AddWithValue("@EndTime", record.EndTime.ToString());
                    command.Parameters.AddWithValue("@BreakMinutes", record.BreakMinutes);

                    command.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 获取指定日期范围内的工作记录
        /// </summary>
        public List<WorkRecord> GetRecordsByDateRange(int employeeId, DateTime startDate, DateTime endDate)
        {
            var records = new List<WorkRecord>();

            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                SELECT * FROM WorkRecords 
                WHERE EmployeeId = @EmployeeId 
                AND WorkDate BETWEEN @StartDate AND @EndDate
                ORDER BY WorkDate, StartTime";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@EmployeeId", employeeId);
                    command.Parameters.AddWithValue("@StartDate", startDate.ToString("yyyy-MM-dd"));
                    command.Parameters.AddWithValue("@EndDate", endDate.ToString("yyyy-MM-dd"));

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            records.Add(new WorkRecord
                            {
                                Id = Convert.ToInt32(reader["Id"]),
                                EmployeeId = Convert.ToInt32(reader["EmployeeId"]),
                                WorkDate = DateTime.Parse(reader["WorkDate"].ToString()),
                                StartTime = TimeSpan.Parse(reader["StartTime"].ToString()),
                                EndTime = TimeSpan.Parse(reader["EndTime"].ToString()),
                                BreakMinutes = Convert.ToInt32(reader["BreakMinutes"]),
                                IsSettled = Convert.ToInt32(reader["IsSettled"]) == 1
                            });
                        }
                    }
                }
            }

            return records;
        }

        /// <summary>
        /// 标记工作记录为已结算
        /// </summary>
        public void MarkRecordsAsSettled(List<int> recordIds)
        {
            if (recordIds == null || recordIds.Count == 0)
                return;

            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = $"UPDATE WorkRecords SET IsSettled = 1 WHERE Id IN ({string.Join(",", recordIds)})";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
        /// <summary>
        /// 更新工作记录
        /// </summary>
        /// <param name="record">要更新的工作记录</param>
        public void UpdateWorkRecord(WorkRecord record)
        {
            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();
                using (var command = new SQLiteCommand(
                    "UPDATE WorkRecords SET WorkDate=@WorkDate, StartTime=@StartTime, " +
                    "EndTime=@EndTime, BreakMinutes=@BreakMinutes " +
                    "WHERE Id=@Id", connection))
                {
                    command.Parameters.AddWithValue("@Id", record.Id);
                    command.Parameters.AddWithValue("@WorkDate", record.WorkDate);
                    command.Parameters.AddWithValue("@StartTime", record.StartTime.ToString());
                    command.Parameters.AddWithValue("@EndTime", record.EndTime.ToString());
                    command.Parameters.AddWithValue("@BreakMinutes", record.BreakMinutes);

                    command.ExecuteNonQuery();
                }
            }
        }
    }
}
